#!/usr/bin/env python

"""Setup Dingo for packaging or installation.

Much here is borrowed from the Python wiki or wxPython.
See U{http://wiki.python.org/moin/DistUtilsTutorial}
"""

__author__ = 'Sarah Mount <s.mount@wlv.ac.uk>'
__credits__ = 'Donn Ingle, Robin Dunn'

import distutils, distutils.command, distutils.command.install_data
import os, sys, glob, fnmatch

def opj(*args):
	path = os.path.join(*args)
	return os.path.normpath(path)

class wx_smart_install_data(distutils.command.install_data.install_data):
    """Need to change self.install_dir to the actual library dir"""
    def run(self):
        install_cmd = self.get_finalized_command('install')
        self.install_dir = getattr(install_cmd, 'install_lib')
        return distutils.command.install_data.install_data.run(self)

def find_data_files(srcdir, *wildcards, **kw):
    """Get a list of all files under the srcdir matching wildcards,
    returned in a format to be used for install_data
    """
    def walk_helper(arg, dirname, files):
        if '.svn' in dirname:
            return
        names = []
        lst, wildcards = arg
        for wc in wildcards:
            wc_name = opj(dirname, wc)
            for f in files:
                filename = opj(dirname, f)

                if fnmatch.fnmatch(filename, wc_name) and not os.path.isdir(filename):
                    names.append(filename)
        if names:
            lst.append( (dirname, names ) )

    file_list = []
    recursive = kw.get('recursive', True)
    if recursive:
        os.path.walk(srcdir, walk_helper, (file_list, wildcards))
    else:
        walk_helper((file_list, wildcards),
                    srcdir,
                    [os.path.basename(f) for f in glob.glob(opj(srcdir, '*'))])
    return file_list

        
files = find_data_files('Dingo/', '*.*')

from distutils.core import setup

setup(name = "dingo",
      version = "0.4",
      description = "Dingo is a development tool for wireless sensor networks.",
      author = 'Sarah Mount, Daniel Goldsmith, John Perks, John Kemp',
      author_email = "s.mount@wlv.ac.uk",
      url = "http://code.google.com/p/dingo-wsn/",
      long_description = """Dingo is a development tool for wireless sensor networks, in development at the University of Wolverhampton, UK.""",


      packages = ['Dingo',
		  'Dingo.Project',
		  # Dingo tools
		  'Dingo.discover',
		  'Dingo.calibrate',
		  # Available models
		  'Dingo.models',
		  'Dingo.models.CPUTime',
		  'Dingo.models.CSV',
		  'Dingo.models.FreeSpace',
		  'Dingo.models.Random',
		  'Dingo.models.TwoRayGroundReflection',
		  'Dingo.models.FloodRouter',
		  'Dingo.models.MuMHR',
		  'Dingo.models.OneHopP2P',
		  # Available plugins
                  'Dingo.plugins',  	
		  'Dingo.plugins.debug',
                  'Dingo.plugins.adcevents'],

      data_files = files,
      

      ## Borrowed from wxPython:
      ## Causes the data_files to be installed into the modules directory.
      ## Override some of the default distutils command classes with my own.
      cmdclass = { 'install_data': wx_smart_install_data },

      scripts = ['scripts/dingo',
                 'scripts/dingo-discover',
                 'scripts/dingo-top',
		 'scripts/dingo-tos',
		 'scripts/dingo-calibrate'],

) 
